package com.rk.recommand;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSClient {

    private static final String HDFS = "hdfs://192.168.45.101:9000";
    private Configuration configuration;


    public HDFSClient(){
        this.configuration = new Configuration();
    }

    public FileSystem getFileSystem() throws URISyntaxException, IOException, InterruptedException {
        return FileSystem.get(new URI(HDFS), this.configuration, "root");
    }

    /**
     * 创建目录
     * @param folder
     * @throws IOException
     */
    public void mkdirs(String folder) throws IOException, URISyntaxException, InterruptedException {
        Path path = new Path(folder);
        FileSystem fs = getFileSystem();
        if (!fs.exists(path)) {
            fs.mkdirs(path);
            System.out.println("Create: " + folder);
        }
        fs.close();
    }

    /**
     * 删除目录
     * @param folder
     * @throws IOException
     */
    public void rmr(String folder) throws IOException, URISyntaxException, InterruptedException {
        Path path = new Path(folder);
        FileSystem fs = getFileSystem();
        fs.deleteOnExit(path);
        System.out.println("Delete: " + folder);
        fs.close();
    }

    /**
     * 查看目录
     * @param folder
     * @throws IOException
     */
    public void ls(String folder) throws IOException, URISyntaxException, InterruptedException {
        Path path = new Path(folder);
        FileSystem fs = getFileSystem();
        FileStatus[] list = fs.listStatus(path);
        System.out.println("ls: " + folder);
        System.out.println("==========================================================");
        for (FileStatus f : list) {
            System.out.printf("name: %s, folder: %s, size: %d\n", f.getPath(), f.isDir(), f.getLen());
        }
        System.out.println("==========================================================");
        fs.close();
    }

    /**
     * 创建文件
     * @param file
     * @param content
     * @throws IOException
     */
    public void createFile(String file, String content) throws IOException, URISyntaxException, InterruptedException {
        byte[] buff = content.getBytes();
        FileSystem fs = getFileSystem();
        FSDataOutputStream os = null;
        try {
            os = fs.create(new Path(file));
            os.write(buff, 0, buff.length);
            System.out.println("Create: " + file);
        } finally {
            if (os != null)
                os.close();
        }
        fs.close();
    }

    /**
     * 上传文件
     * @param filePath
     * @param remotePath
     * @throws IOException
     */
    public void uploadFile(String filePath, String remotePath) throws IOException, URISyntaxException, InterruptedException {
        FileSystem fs = getFileSystem();
        fs.copyFromLocalFile(new Path(filePath), new Path(remotePath));
        fs.close();
    }

    public void download(String remote, String local) throws InterruptedException, IOException, URISyntaxException {
        Path path = new Path(remote);
        FileSystem fs = getFileSystem();
        fs.copyToLocalFile(path, new Path(local));
        System.out.println("download: from" + remote + "to" + local);
        fs.close();
    }

    /**
     * 查看文件
     * @param remoteFile
     * @throws IOException
     */
    public void cat(String remoteFile) throws IOException, URISyntaxException, InterruptedException {
        Path path = new Path(remoteFile);
        FileSystem fs = getFileSystem();
        FSDataInputStream fsdis = null;
        System.out.println("cat: " + remoteFile);
        try {
            fsdis =fs.open(path);
            IOUtils.copyBytes(fsdis, System.out, 4096, false);
        } finally {
            IOUtils.closeStream(fsdis);
            fs.close();
        }
    }

}
